package com.example.utils;

import com.example.system.entity.User;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

@Component
public class JwtUtils {
    //有效期
    private static final long JWT_EXPIRE = 30 * 60 * 1000L;
    //令牌秘钥
    private static final String JWT_KEY = "123456";

    public String createToken(User user) {

        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtToken = jwtBuilder
        //header
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
                //payload
                .claim("username",user.getName())
                .setExpiration(new Date(System.currentTimeMillis()+JWT_EXPIRE))
                .setId(UUID.randomUUID().toString())
                //signature
                .signWith(SignatureAlgorithm.HS256,JWT_KEY)
                .compact();
        return jwtToken;

    }
}
